package com.imooc.authorization.dao;

import com.imooc.authorization.common.BaseDAO;
import com.imooc.authorization.entity.Function;
import com.imooc.authorization.entity.Role;
import com.imooc.authorization.entity.RoleFunction;
import com.imooc.authorization.entity.UserRole;
import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2016/8/21.
 */

@Repository
public class UserRoleDAO extends BaseDAO {


    private class UserRoleMapper implements RowMapper<UserRole> {


        @Override
        public UserRole mapRow(ResultSet rs, int rowNum) throws SQLException {

            UserRole userRole = new UserRole();
            userRole.setId(rs.getLong("id"));
            userRole.setRoleId(rs.getLong("role_id"));
            userRole.setUserId(rs.getLong("user_id"));

            return userRole;
        }
    }




    /**
     * 根据ID查询
     * @param id 用户角色对应关系 ID
     * @return 用户角色对应关系
     */
    public UserRole findUserRoleById(Long id){
        String sql ="select * from auth_user_role where id=?";

        return jdbcTemplate.queryForObject(sql,new Object[]{id},new UserRoleMapper());
    }

    /**
     * 保存用户角色对应关系
     * @param userRole 用户角色对应关系
     */
    public void saveUserRole(UserRole userRole){

        String sql = "insert into auth_user_role(id,role_id,user_id) values(?,?,?)";

        jdbcTemplate.update(sql,userRole.getId(),userRole.getUserId(),userRole.getUserId());
    }

    /**
     * 根据ID删除用户角色对应关系
     * @param userId 用户角色对应关系ID
     */

    public void deleteByUserId(Long userId){
        String sql = "delete from auth_user_role where user_id =?";

        jdbcTemplate.update(sql,userId);

    }

    /**
     * 分页查询用户角色对应关系
     * @param page 页码
     * @param size 每页记录数
     * @return 用户角色对应关系集合
     */
    public List<UserRole> findUserRoles(int page,int size){
        String sql ="select * from auth_user_role order by user_id limit ?,?";


        return jdbcTemplate.query(sql,new Object[]{(page-1)*size,size},new UserRoleMapper());
    }

    /**
     * 根据用户ID查询用户角色对应关系（集合）
     * @param userId 用户ID
     * @return 用户角色对应关系集合
     */

    public List<UserRole> findUserRoleByUserId(Long userId){

        String sql ="select * from auth_user_role where user_id=?";

        return jdbcTemplate.query(sql,new Object[]{userId},new UserRoleMapper());
    }

    /**
     * 保存用户角色对应关系集合
     * @param userRoles 用户角色对应关系集合
     */
    public void saveUserRoles(Collection<UserRole> userRoles){

        String sql ="insert into auth_user_role(user_id,role_id) values(?,?)";
        List<Object[]> batchArgs = new ArrayList<>();

        userRoles.forEach((url) -> {
            Object[] obj = new Object[2];
            obj[0]=url.getUserId();
            obj[10]=url.getRoleId();

            batchArgs.add(obj);


        });

        jdbcTemplate.batchUpdate(sql,batchArgs);
    }


}
